/*-
 * Copyright (c) 2013 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ MTBLAS: cor_ctrl.h    Rev. 0.0.7    2013-10-31T00:10+08:00 $
 */

#include <stddef.h>

#include <bscerr.h>

#include <cor_ctrl.h>

int cor_ctrl_gemv_flag(const int rdr, const int trn, int *flag)
{
    int ierr = ERRNO_UNDEFINED;

    if ((rdr == MTBLAS_ROW_MAJOR && trn == MTBLAS_TRANS_NONE) || (rdr == MTBLAS_COL_MAJOR && trn == MTBLAS_TRANS) || (rdr == MTBLAS_COL_MAJOR && trn == MTBLAS_TRANS_CONJ))
    {
        *flag = GEMV_CATEGORY_0;
    }
    else if ((rdr == MTBLAS_ROW_MAJOR && trn == MTBLAS_TRANS) || (rdr == MTBLAS_ROW_MAJOR && trn == MTBLAS_TRANS_CONJ) || (rdr == MTBLAS_COL_MAJOR && trn == MTBLAS_TRANS_NONE))
    {
        *flag = GEMV_CATEGORY_1;
    }
    else
    {
        *flag = ERRNO_UNKNOWN_TYPE;
        ierr = ERRNO_UNKNOWN_TYPE;
    }

    return ierr;
}


int cor_ctrl_gemv_size(const int trn, const size_t rz, const size_t cz, size_t *r_sz, size_t *c_sz)
{
    int ierr = ERRNO_UNDEFINED;

    if (trn == MTBLAS_TRANS_NONE)
    {
        *r_sz = rz;
        *c_sz = cz;
    }
    else if (trn == MTBLAS_TRANS || trn == MTBLAS_TRANS_CONJ)
    {
        *r_sz = cz;
        *c_sz = rz;
    }
    else
    {
        ierr = ERRNO_UNKNOWN_TYPE;
    }

    return ierr;
}


int cor_ctrl_gemm_flag(const int rdr, const int trna, const int trnb, int *flag)
{
    int ierr = ERRNO_UNDEFINED;

    if (rdr == MTBLAS_ROW_MAJOR)
    {
        if (trna == MTBLAS_TRANS_NONE)
        {
            if (trnb == MTBLAS_TRANS_NONE)
            {
                *flag = GEMM_CATEGORY_0;
            }
            else if (trnb == MTBLAS_TRANS || trnb == MTBLAS_TRANS_CONJ)
            {
                *flag = GEMM_CATEGORY_1;
            }
        }
        else if (trna == MTBLAS_TRANS || trna == MTBLAS_TRANS_CONJ)
        {
            if (trnb == MTBLAS_TRANS_NONE)
            {
                *flag = GEMM_CATEGORY_2;
            }
            else if (trnb == MTBLAS_TRANS || trnb == MTBLAS_TRANS_CONJ)
            {
                *flag = GEMM_CATEGORY_3;
            }
        }
    }
    else if (rdr == MTBLAS_COL_MAJOR)
    {
        if (trna == MTBLAS_TRANS_NONE)
        {
            if (trnb == MTBLAS_TRANS_NONE)
            {
                *flag = GEMM_CATEGORY_4;
            }
            else if (trnb == MTBLAS_TRANS || trnb == MTBLAS_TRANS_CONJ)
            {
                *flag = GEMM_CATEGORY_5;
            }
        }
        else if (trna == MTBLAS_TRANS || trna == MTBLAS_TRANS_CONJ)
        {
            if (trnb == MTBLAS_TRANS_NONE)
            {
                *flag = GEMM_CATEGORY_6;
            }
            else if (trnb == MTBLAS_TRANS || trnb == MTBLAS_TRANS_CONJ)
            {
                *flag = GEMM_CATEGORY_7;
            }
        }
    }
    else
    {
        *flag = ERRNO_UNKNOWN_TYPE;
        ierr =  ERRNO_UNKNOWN_TYPE;
    }

    return ierr;
}
